<script setup lang="ts">
import {
  getAddress,
  getMedicineOrderPre,
  getMedicineOrderNo
} from '@/services/order'
import type { AddressItem, OrderPre } from '@/types/order'
import { showToast } from 'vant'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()

const loading = ref<boolean>(false)
// 支付弹框
const isShow = ref<boolean>(false)
const orderId = ref<string>('')
const isAgree = ref<boolean>(false)

// 地址信息
const addressinfo = ref<AddressItem>()
// 药品订单预览信息
const medicineOrderPre = ref<OrderPre>()

const initAddress = async () => {
  const { data } = await getAddress()
  addressinfo.value = data[0]
}
const initMedicineOrderPre = async () => {
  const { data } = await getMedicineOrderPre({
    prescriptionId: route.query.id as string
  })
  medicineOrderPre.value = data
}
initAddress()
initMedicineOrderPre()

const onSubmit = async () => {
  // 判断是否同意协议
  if (!isAgree.value) return showToast('请同意协议')
  if (!addressinfo.value!.id) return showToast('请选择地址')
  if (!medicineOrderPre.value?.id) return showToast('未找到处方')

  loading.value = true

  try {
    // 发请求生成药品订单
    const { data } = await getMedicineOrderNo({
      id: route.query.id as string,
      couponId: medicineOrderPre.value?.couponId as string,
      addressId: addressinfo.value?.id as string
    })
    orderId.value = data.id
    // 弹出支付选框
    isShow.value = true
  } catch (error) {
    // 失败
  } finally {
    // 关闭loading
    loading.value = false
  }
}
</script>

<template>
  <div class="order-pay-page" v-if="addressinfo && medicineOrderPre">
    <cp-nav-bar title="药品支付" />
    <div class="order-address">
      <p class="area">
        <van-icon name="location" />
        <span
          >{{ addressinfo.province }} {{ addressinfo.city }}
          {{ addressinfo.county }}</span
        >
      </p>
      <p class="detail">{{ addressinfo.addressDetail }}</p>
      <p>
        {{ addressinfo.receiver }}
        <!-- 下面正则中的小括号表示分组 获取分组中的信息$1 $2... -->
        <!-- {{ addressinfo.mobile.replace(/^(\d{3})\d+(\d{4})$/g, '$1****$2') }} -->
        {{
          addressinfo.mobile.slice(0, 3) + '****' + addressinfo.mobile.slice(-4)
        }}
      </p>
    </div>
    <div class="order-medical">
      <div class="head">
        <h3>优医药房</h3>
        <small>优医质保 假一赔十</small>
      </div>
      <div
        class="item van-hairline--top"
        v-for="med in medicineOrderPre.medicines"
        :key="med.id"
      >
        <img class="img" src="@/assets/ad.png" alt="" />
        <div class="info">
          <p class="name">
            <span>{{ med.name }}</span>
            <span>x {{ med.quantity }}</span>
          </p>
          <p class="size">
            <van-tag>{{
              med.prescriptionFlag === 1 ? '处方药' : '非处方药'
            }}</van-tag>
            <span>80ml</span>
          </p>
          <p class="price">￥{{ med.amount }}</p>
        </div>
        <div class="desc" v-if="med.usageDosag">
          用法用量：{{ med.usageDosag }}
        </div>
      </div>
    </div>
    <div class="order-detail">
      <van-cell-group>
        <van-cell title="药品金额" :value="`￥${medicineOrderPre?.payment}`" />
        <van-cell title="运费" :value="`￥${medicineOrderPre?.expressFee}`" />
        <van-cell
          title="优惠券"
          :value="`-￥${medicineOrderPre?.couponDeduction}`"
        />
        <van-cell
          title="实付款"
          :value="`￥${medicineOrderPre?.actualPayment}`"
          class="price"
        />
      </van-cell-group>
    </div>
    <div class="order-tip">
      <p class="tip">
        由于药品的特殊性，如非错发、漏发药品的情况，药品一经发出
        不得退换，请核对药品信息无误后下单。
      </p>
      <van-checkbox v-model="isAgree"
        >我已同意<a href="javascript:;">支付协议</a></van-checkbox
      >
    </div>
    <van-submit-bar
      :price="medicineOrderPre?.actualPayment! * 100"
      button-text="立即支付"
      button-type="primary"
      text-align="left"
      :loading="loading"
      @submit="onSubmit"
    ></van-submit-bar>
  </div>
  <div class="order-pay-page" v-else>
    <cp-nav-bar title="药品支付" />
    <van-skeleton title :row="4" style="margin-top: 10px" />
    <van-skeleton title :row="4" style="margin-top: 10px" />
  </div>

  <!-- 支付成功以后，跳转到 /order/pay/result -->
  <CpPaySheet
    v-if="medicineOrderPre"
    :order-id="orderId"
    v-model:show="isShow"
    :actualPayment="medicineOrderPre?.actualPayment"
    callback-url="/order/pay/result"
  ></CpPaySheet>
</template>

<style lang="scss" scoped>
:deep(.van-nav-bar) {
  background-color: var(--cp-primary);
  .van-nav-bar__arrow,
  .van-nav-bar__title {
    color: #fff;
  }
}
:deep(.van-cell) {
  .van-cell__title {
    font-size: 16px;
  }
  .van-cell__value {
    font-size: 16px;
  }
  &.price {
    .van-cell__value {
      font-size: 18px;
      color: var(--cp-price);
    }
  }
}
:deep(.van-submit-bar) {
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
  .van-button {
    width: 200px;
  }
}
.order-pay-page {
  padding: 46px 0 65px;
}
.order-address {
  padding: 15px 15px 0 15px;
  background-color: #fff;
  font-size: 13px;
  .area {
    color: var(--cp-tag);
    margin-bottom: 5px;
    .van-icon-location {
      color: #ff7702;
      font-size: 14px;
    }
  }
  .detail {
    font-size: 17px;
    margin-bottom: 5px;
  }
  &::after {
    content: '';
    display: block;
    height: 12px;
    background-color: var(--cp-bg);
    margin: 0 -15px;
    margin-top: 15px;
  }
}

.order-medical {
  background-color: #fff;
  padding: 0 15px;
  .head {
    display: flex;
    height: 54px;
    align-items: center;
    > h3 {
      font-size: 16px;
      font-weight: normal;
    }
    > small {
      font-size: 13px;
      color: var(--cp-tag);
      margin-left: 10px;
    }
  }
  .item {
    display: flex;
    flex-wrap: wrap;
    padding: 15px 0;
    .img {
      width: 80px;
      height: 70px;
      border-radius: 2px;
      overflow: hidden;
    }
    .info {
      padding-left: 15px;
      width: 250px;
      .name {
        display: flex;
        font-size: 15px;
        margin-bottom: 5px;
        > span:first-child {
          width: 200px;
        }
        > span:last-child {
          width: 50px;
          text-align: right;
        }
      }
      .size {
        margin-bottom: 5px;
        .van-tag {
          background-color: var(--cp-primary);
          vertical-align: middle;
        }
        span:not(.van-tag) {
          margin-left: 10px;
          color: var(--cp-tag);
          vertical-align: middle;
        }
      }
      .price {
        font-size: 16px;
        color: #eb5757;
      }
    }
    .desc {
      width: 100%;
      background-color: var(--cp-bg);
      border-radius: 4px;
      margin-top: 10px;
      padding: 4px 10px;
      color: var(--cp-tip);
    }
  }
}
.order-tip {
  padding: 0 15px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 10px;
  .tip {
    font-size: 12px;
    color: var(--cp-tag);
    width: 100%;
    &::before {
      content: '*';
      color: var(--cp-price);
      font-size: 14px;
    }
    margin-bottom: 30px;
  }
  .van-checkbox {
    a {
      color: var(--cp-primary);
    }
  }
}
</style>
